import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)
const store=new Vuex.Store({
    mutations:{
        INIT_DATA(state,data){
            state.sessions.push({storeName:"上海维修厂addd",phoneNumber:data,userId:"addd"})
            state.sessions.push({storeName:"北京维修厂admin",phoneNumber:data,userId:"admin"})
            state.replies.push("自动回复1 abcde","回复2 uiop")

        //     const room={serviceName:"客服admin",phoneNumber:123456,data,serviceId:"admin"}
        //     const newRoom={
        //         room,
        //         msg:[
        //             {
        //                 content:Math.random(),
        //                 date:'2016-12-12',
        //                 self:true
        //             },
        //             {
        //                 content:Math.random(),
        //                 date:'2016-12-12',
        //                 self:false
        //             },
        //         ],
        //         alive:true,
        //         roomId:Math.random(),
        //         serviceId:'admin'
        //     }

        //     state.serviceList.unshift(newRoom)
        },
        ACCEPT_ROOM:(state,room)=>{//接收房间请求
            const newRoom={
                room,
                msg:[
                    {
                        content:Math.random(),
                        date:'2016-12-12',
                        self:true
                    },
                    {
                        content:Math.random(),
                        date:'2016-12-12',
                        self:false
                    },
                ],
                alive:true,
                roomId:Math.random(),
                userId:room.userId
            }
            state.chatList.unshift(newRoom);
            state.selectId=newRoom.userId
            state.sessions.splice(state.sessions.indexOf(room),1)
        },
        REJECT_ROOM:(state,room)=>{//拒绝房间请求
            state.sessions.splice(state.sessions.indexOf(room),1)
        },
        CHANGE_SELECT_ID:(state,id)=>{//选中聊天列表
            state.selectId=id
            console.log(id)
        },
        CHANGE_SELECT_SERVICE:(state,id)=>{//选中聊天列表
            state.selectService=id
            console.log(id)
            let selectedChatSession=state.serviceList.find(session=>session.serviceId===state.selectService)
            console.log(selectedChatSession)
        },
        APPEND_CHAT_MESSAGE:(state,obj)=>{//接收服务器消息 添加到聊天列表
            let session=state.chatList.find(a=>a.userId===obj.fromId)
            session.msg.push({
                content:obj.msg,
                date:new Date(),
                self:false
            })
        },
        APPEND_SERVICE_MESSAGE:(state,obj)=>{//接收客服消息
            let session=state.serviceList.find(a=>a.serviceId===obj.fromId)
            session.msg.push({
                content:obj.msg,
                date:new Date(),
                self:false
            })
        },
        APPEND_ONLINE_SERVICES:(state,obj)=>{
            obj.forEach(function (v){
                state.serviceList.unshift({
                    msg:[
                        {
                            content:Math.random(),
                            date:'2016-12-12',
                            self:true
                        },
                        {
                            content:Math.random(),
                            date:'2016-12-12',
                            self:false
                        },
                    ],
                    alive:true,
                    roomId:Math.random(),
                    serviceId:v.id,
                    serviceName:v.name
                })
            })
        }
    },
    actions:{
        INIT_DATA:()=>{
            
        },
    },
    state:{
        chatList:[],//聊天列表
        selectId:'',
        sessions:[],//新会话
        online:'',//是否在线
        replies:[],//快捷回复
        //客服状态
        serviceList:[],
        selectService:'',
    },
    getters:{
        // selected:state=>{
        //     return state.chatList.find(a=>a.roomId===state.selectId);
        // },
        // selectId:state=>{
        //     return state.selectId
        // }
    }
})
export default store
